﻿/**
 * nenglong.mvc.form.js
 * Library v0.0.1
 *
 * Copyright (c) 2003 - 2018, nenglong
 * All rights reserved.
 *
 * Date: 2013-06-30
 * 依赖于：nenglong.mvc.js
 */
$NL.defined("NengLong.Mvc.Extend", ["js/core/nenglong.mvc.js", "js/core/nenglong.util.js"], function () {
    /**
     *mvc的form扩展
     */
    $NL.cls("static Form", function () {
        this.elAdaptor = {
            "input": {
                "get": function ($el) {
                    var ret = null;
                    switch ($el.attr("type").toLowerCase()) {
                        case "checkbox":
                            ret = !!$el.get(0).checked;
                            break;
                        default:
                            ret = $el.val();
                            break;
                    }
                    return ret;
                },
                "set": function ($el, val) {
                    switch ($el.attr("type").toLowerCase()) {
                        case "checkbox":
                            $el.attr("checked", val);
                            break;
                        default:
                            $el.val(val);
                            break;
                    }
                }
            },
            "div": {
                "get": function ($el) {
                    return $el.html();
                },
                "set": function ($el, val) {
                    $el.html(val);
                }
            },
            "span": {
                "get": function ($el) {
                    return $el.html();
                },
                "set": function ($el, val) {
                    $el.html(val);
                }
            },
            "textarea": {
                "get": function ($el) {
                    return $el.val();
                },
                "set": function ($el, val) {
                    $el.val(val);
                }
            }
        };
        NengLong.Mvc.Page.Extend.viewExtend("getForm", function (formId, elAdaptor) {
            if (isUndefined(elAdaptor)) elAdaptor = {};
            var ada = {};
            $.extend(ada, NengLong.Mvc.Extend.Form.elAdaptor, elAdaptor);
            var $form = null;
            if (isUndefined(formId)) {
                $form = this.get$C().find("form").first();
            }
            else
                $form = this.$[formId] || $("[data-id=" + formId + "]") || $("#" + formId);
            if (isUndefined($form) || $form.length == 0) {
                $NL.log.e("找不到from,formId=" + formId);
                return null;
            }
            var viewClassName=$form.attr("viewClassName") || $form.parents("[parentClassName]").first().attr("parentClassName")
            var val = {};
            $form.first().find("[data-server]").each(function () {
                var $el = $(this);
                var pClassName=$el.attr("viewClassName") || $el.parents("[parentClassName]").first().attr("parentClassName");
                if(viewClassName !=pClassName) return true;
                var key = $el.attr("data-server");
                if(!key ||$.trim(key).length<=0) return;
                var id = $el.attr("id");
                var tagName = this.tagName.toLowerCase();
                var isMvcOrUserControl = !isUndefined($el.attr("data-control") || $el.attr("data-usercontrol"));
                if (!isUndefined(ada[id]) && $.isFunction(ada[id].get)) {
                    val[key] = ada[id].get($el);
                }
                else if (isMvcOrUserControl) {
                    try{
                        val[key] = $el.data("control").getF();
                    }
                    catch (err) {
                        $NL.log.i("key="+key+" 设置getF时出错！");
                    }
                }
                else if (!isUndefined(ada[tagName]) && $.isFunction(ada[tagName].get)) {
                    val[key] = ada[tagName].get($el);
                }
                else {
                    $NL.log.e("无法处理该form控件的值，data-server=" + key);
                }
            });
            return val;

        });
        NengLong.Mvc.Page.Extend.viewExtend("setForm", function (val, formId, elAdaptor) {
            if (isUndefined(elAdaptor)) elAdaptor = {};
            var ada = {};
            $.extend(ada, NengLong.Mvc.Extend.Form.elAdaptor, elAdaptor);
            var $form = null;
            if (isUndefined(formId)) {
                $form = this.get$C().find("form").first();
            }
            else
                $form = this.$[formId] || $("[data-id=" + formId + "]") || $("#" + formId);
            if (isUndefined($form) || $form.length == 0) {
                $NL.log.e("找不到from,formId=" + formId);
                return null;
            }
            var viewClassName= $form.attr("viewClassName") || $form.parents("[parentClassName]").first().attr("parentClassName");
            $form.first().find("[data-server]").each(function () {
                var $el = $(this);
                var pClassName=$el.attr("viewClassName") || $el.parents("[parentClassName]").first().attr("parentClassName");
                if(viewClassName !=pClassName) return true;
                var key = $el.attr("data-server");
                var id = $el.attr("id");
                var tagName = this.tagName.toLowerCase();
                var isMvcControl = !isUndefined($el.attr("data-control") || $el.attr("data-usercontrol"));
                if (!isUndefined(ada[id]) && $.isFunction(ada[id].set)) {
                    ada[id].set($el, val[key]);
                }
                else if (isMvcControl) {
                    $el.data("control").setF(val[key]);
                }
                else if (!isUndefined(ada[tagName]) && $.isFunction(ada[tagName].set)) {
                    ada[tagName].set($el, val[key]);
                }
                else {
                    $NL.log.e("无法处理该form控件的值，data-server=" + key);
                }
            });

        });
    });
});